package com.hongshan.wallet.core.home

import android.app.Application
import android.databinding.ObservableField
import android.view.View
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.R
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.core.trading.TradingRecycleAdapter
import com.hongshan.wallet.http.HttpClient
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.router.go
import com.hongshan.wallet.toast
import com.hongshan.wallet.tryRun
import com.hongshan.wallet.util.checkRealNameAndPwdStatus
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent


/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : Administrator
 * @data     : 2019/3/20
 * @email    : zhangxx@baihang-china.com
 */
class HomeViewModel(application: Application) : MyViewModel(application) {

    var adapter: TradingRecycleAdapter? = null

    var pagerAdapter = ObservableField(BannerPagerAdapter(null))

    val money = ObservableField<String>(MyApplication.userInfo.get()?.balance)
    val freezeMoney = ObservableField<String>("${MyApplication.userInfo.get()?.frozen}元已冻结")
    val showStatus = ObservableField<Int>(R.drawable.ic_eye_o)
    val onLineStatus = ObservableField<Int>(R.drawable.ic_switch_c)

    val onLineStatusText = ObservableField<String>("账户离线,不可接单")

    val onLineEnable = ObservableField(true)

    val showDesc = SingleLiveEvent<Unit>()

    val showAlert = SingleLiveEvent<Array<String>>()

    fun onRefresh() {
        GlobalScope.launch(Dispatchers.Main) {
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun {
                    HttpClient.httpApi.getOrderList(status = 0).execute()
                }
            }
            val body = result.await()?.body()
            if (body?.code == 0) {
                adapter?.list?.clear()
                adapter?.list?.addAll(body.data.list)
                adapter?.notifyDataSetChanged()
            }
        }
    }

    val changeShowStatus = View.OnClickListener {
        val isShow = showStatus.get() == R.drawable.ic_eye_o
        if (isShow){
            showStatus.set(R.drawable.ic_eye_c)
            money.set("*****")
            freezeMoney.set("*****元已冻结")
        } else {
            showStatus.set(R.drawable.ic_eye_o)
            money.set(MyApplication.userInfo.get()?.balance)
            freezeMoney.set("${MyApplication.userInfo.get()?.frozen}元已冻结")
        }
    }
    val withdrawal = View.OnClickListener {
        if (checkRealNameAndPwdStatus()) return@OnClickListener
        go(RouterIndex.WITHDRAWAL)
    }
    val moneyDescription = View.OnClickListener {
        go(RouterIndex.BALANCE_DESC)
    }
    val onLine = View.OnClickListener {
        val isOnLine = onLineStatus.get() == R.drawable.ic_switch_o
        changeStatus(isOnLine)
    }

    private fun changeStatus(isOnLine: Boolean) {
        if (checkRealNameAndPwdStatus()) return
        onLineEnable.set(false)
        GlobalScope.launch(Dispatchers.Main) {
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun {
                    HttpClient.httpApi.updateOrderReceiveState(if (isOnLine) 0 else 1).execute()
                }
            }
            val body = result.await()?.body()
            onLineEnable.set(true)
            when(body?.code) {
                0 -> if (isOnLine) {
                    onLineStatus.set(R.drawable.ic_switch_c)
                    onLineStatusText.set("账户离线,不可接单")
//                    MyActivityManager.activityStack.forEach { it.cancleScreenOn() }
                } else {
                    onLineStatus.set(R.drawable.ic_switch_o)
                    onLineStatusText.set("账户上线,即可自动接单")
                   showAlert.value = arrayOf("已开启接单", "如有连续多笔失效订单,将自动关闭接单；", "确认")
//                    MyActivityManager.activityStack.forEach { it.screenAlwaysOn() }
                }
                -2,-3 ->{
                    toast(body.msg)
                    go(RouterIndex.ACCOUNT_MANAGE)
                }
            }
        }
    }

    val topUp = View.OnClickListener {
        if (checkRealNameAndPwdStatus()) return@OnClickListener
        go(RouterIndex.PAY_METHOD)
    }

    val questionClick = View.OnClickListener {
        showDesc.call()
    }

    fun getBanner(){
        GlobalScope.launch(Dispatchers.Main) {
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun { HttpClient.httpApi.getBanner("1").execute() }
            }
            val response = result.await() ?: return@launch
            if (response.isSuccessful) {
                if (response.body()?.code == 0) {
                    pagerAdapter.get()?.bannerList = response.body()?.data
                    pagerAdapter.notifyChange()
                } else{
                    toast(response.body()?.msg)
                }
            }
        }
    }
}


